﻿using CheatMenu.Managers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using CheatMenu.Utils;
using Fungus;

namespace CheatMenu.Menus
{
    public class MenuTextEditor : MenuBase
    {
        public delegate void TextEditorButtonClickedDelegate(MenuTextEditor sender, DialogResult pressedButton);

        public event TextEditorButtonClickedDelegate ButtonClicked;
        protected void OnButtonClicked(DialogResult pressedButton)
        {
            Shown = false;
            ButtonClicked?.Invoke(this, pressedButton);
        }

        private GUIStyle defaultFontLabelStyle;
        private GUIStyle defaultFontLabelHeaderStyle;
        private GUIStyle defaultFontControlWidthHeightButtonStyle;
        private GUIStyle defaultTextFieldStyle;

        public string PromptText { get; set; }
        public string Result { get; set; }
        public StringVariable Variable { get; set; }
        public Flowchart Chart { get; set; }

        public MenuTextEditor(string name, string text = null) : base(name, text)
        {
            Alignment = Alignment.Centre;
        }

        protected override Vector2 InitializeSize()
        => new Vector2((GUIManager.ControlWidth * 3) + (GUIManager.PaddingSize * 2), (GUIManager.ControlHeight * 2) + (GUIManager.PaddingSize * 2));

        protected override void InitializeGUI()
        {
            defaultFontLabelStyle = new GUIStyle(GUI.skin.label)
            {
                fontSize = GUIManager.ControlFontSize,
                normal = new GUIStyleState
                {
                    textColor = GUIManager.LabelColor
                }
            };

            defaultFontLabelHeaderStyle = new GUIStyle(GUI.skin.label)
            {
                fontSize = GUIManager.ControlFontSize,
                fontStyle = FontStyle.Bold,
                normal = new GUIStyleState
                {
                    textColor = GUIManager.LabelHeaderColor
                }
            };

            defaultFontControlWidthHeightButtonStyle = new GUIStyle(GUI.skin.button)
            {
                fontSize = GUIManager.ControlFontSize,
                fixedWidth = GUIManager.ControlWidth,
                fixedHeight = GUIManager.ControlHeight
            };

            defaultTextFieldStyle = new GUIStyle(GUI.skin.textField)
            {
                fontSize = GUIManager.ControlFontSize,
                fixedWidth = GUIManager.ControlWidthTimesTwo,
            };
        }

        public bool Apply()
        {
            bool success = false;

            if(Variable != null && Chart != null)
            {
                Variable.Value = Result;
                Chart.SetStringVariable(Variable.Key, Variable.Value);
                PlayerPrefs.SetString(SetSaveProfile.SaveProfile + "_" + Chart.SubstituteVariables(Variable.Key), Variable.Value);
                success = true;
            }

            return success;
        }

        public void Populate(Flowchart chart, StringVariable stringVariable)
        {
            Chart = chart;
            Variable = stringVariable;
            PromptText = stringVariable.Key;
            Result = stringVariable.Value;
        }

        protected override void MenuWindow(int id)
        {
            GUILayout.BeginVertical();
            {
                GUILayout.BeginVertical(GUI.skin.box);
                {
                    GUILayout.Label("Edit Variable:", defaultFontLabelHeaderStyle);
                    GUILayout.Label(PromptText, defaultFontLabelStyle);
                }
                GUILayout.EndVertical();

                GUILayout.BeginHorizontal();
                {
                    GUILayout.Label("New Value:", defaultFontLabelStyle);
                    GUILayout.FlexibleSpace();
                    Result = GUILayout.TextField(Result, defaultTextFieldStyle);
                }
                GUILayout.EndHorizontal();

                GUILayout.BeginHorizontal();
                {
                    GUILayout.BeginHorizontal(GUI.skin.box);
                    {
                        if (GUILayout.Button(Enum.GetName(typeof(DialogResult), DialogResult.Apply), defaultFontControlWidthHeightButtonStyle))
                            OnButtonClicked(DialogResult.Apply);
                        GUILayout.FlexibleSpace();
                        if (GUILayout.Button(Enum.GetName(typeof(DialogResult), DialogResult.Cancel), defaultFontControlWidthHeightButtonStyle))
                            OnButtonClicked(DialogResult.Cancel);
                    }
                    GUILayout.EndHorizontal();
                }
                GUILayout.EndHorizontal();
            }
            GUILayout.EndVertical();
        }
    }
}
